package com.hitqz.robot.biz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.common.entity.PatrolItem;
import com.hitqz.robot.api.common.entity.PatrolItemMark;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.biz.core.FileProperties;
import com.hitqz.robot.biz.mapper.PatrolItemMarkMapper;
import com.hitqz.robot.biz.service.PatrolItemMarkService;
import com.hitqz.robot.biz.service.PatrolItemService;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.EquipmentLoginParamsDto;
import com.hitqz.robot.camera.dto.MediaFileDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.hitqz.robot.camera.dto.TakePicParamsDto;
import com.hitqz.robot.common.core.constant.CommonConstants;
import com.hitqz.robot.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Objects;

/**
 * @author wengyx
 * @date 2024/8/7
 */
@Service
@AllArgsConstructor
public class PatrolItemMarkServiceIml extends ServiceImpl<PatrolItemMarkMapper, PatrolItemMark> implements PatrolItemMarkService {

    private final PatrolItemService patrolItemService;
    private final RobotEquipmentService robotEquipmentService;
    private final EquipmentFactory factory;
    private final FileProperties fileProperties;


    @Override
    public R<PatrolItemMark> addPicByPatrolItemId(Integer patrolItemId) {
        //获取外设信息
        PatrolItem item = patrolItemService.getById(patrolItemId);
        R<MediaFileDto> result = this.takePic(item.getEquipmentId(),item.getName());
        if (Objects.equals(result.getCode(), CommonConstants.FAIL)){
            return R.failed(result.getMsg());
        }
        //保存标定数据
        PatrolItemMark mark = new PatrolItemMark();
        mark.setPatrolItemId(patrolItemId);
        mark.setUrl(result.getData().getRelativePath());
        this.saveOrUpdate(mark);
        return R.ok(mark);
    }

    @Override
    public R<PatrolItemMark> addPicById(Integer id) {
        PatrolItem item = patrolItemService.getById(id);
        String folder ="mark" + File.separator + item.getId() + File.separator ;
        R<MediaFileDto> result = this.takePic(item.getEquipmentId(),folder);
        if (Objects.equals(result.getCode(), CommonConstants.FAIL)){
            return R.failed(result.getMsg());
        }
        PatrolItemMark mark = this.getOne(Wrappers.<PatrolItemMark>lambdaQuery()
                .eq(PatrolItemMark::getPatrolItemId,id)
                .last("limit 1"));
        mark.setUrl(result.getData().getRelativePath());
        this.saveOrUpdate(mark);
        return R.ok(mark);
    }


    private R<MediaFileDto> takePic(Integer equipmentId,String itemName){
        try {
            if (equipmentId == null) {
                R.failed("检测项未选择外设,无法标定");
            }
            //可见光、红外、USB相机可标定
            RobotEquipment equipment = robotEquipmentService.getById(equipmentId);
            List<Integer> type = List.of(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getType(),
                    EquipmentEnum.INFRARED_CHANNEL.getType(),
                    EquipmentEnum.USB_CAMERA.getType());
            if (!type.contains(equipment.getType())) {
                R.failed("所选外设不支持标定");
            }
            IEquipmentService service = factory.getServiceByBrand(equipment.getBrand());
            EquipmentLoginParamsDto loginParamsDto = new EquipmentLoginParamsDto();
            loginParamsDto.setUsername(equipment.getUsername());
            loginParamsDto.setPort(equipment.getPort());
            loginParamsDto.setPassword(equipment.getPassword());
            loginParamsDto.setIp(equipment.getIp());
            service.login(loginParamsDto);
            // 拍照
            TakePicParamsDto paramsDto = new TakePicParamsDto();
            paramsDto.setBasePath(fileProperties.getLocal().getBasePath());
            paramsDto.setFolder("mark" + File.separator + itemName);
            QueryParamsDto queryParamsDto = new QueryParamsDto();
            queryParamsDto.setUsername(equipment.getUsername());
            queryParamsDto.setPort(equipment.getPort());
            queryParamsDto.setPassword(equipment.getPassword());
            queryParamsDto.setIp(equipment.getIp());
            queryParamsDto.setChannel(equipment.getChannel());
            MediaFileDto mediaFileDto = (MediaFileDto) service.executeCmd(queryParamsDto,  true, EquipmentCmdEnum.TAKE_PIC, paramsDto);
            return R.ok(mediaFileDto);
        }catch (Exception e){
            return R.failed(e.getMessage());
        }
    }
}
